package com.lancoo.znbkxx.teacher.aibkTestCoach.questionToMark

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.auidoPlayer.AudioTracker
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseFragment
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.ItemXMark
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.StuAnswerMark

class MarkFragmentParentView(
        val activity: AibkQuesMarkActivity,
        private val mAudioTracker: AudioTracker? = null,
        private var stuAnswerList: MutableList<StuAnswerMark>,
        private var childIndex: Int, //第几个child ps：综合题child存在多个的情况
        private val index: Int, //第几个答题点
        private val typeNo: String
) : KBaseFragment() {

    protected var isCreate = false
    private var childAnswerList: MutableList<ItemXMark> = ArrayList()
    private lateinit var rvMarkParentView: RecyclerView
    override fun createLayout(): Int {
        return R.layout.aibk_adapter_mark_parent_view
    }

    override fun initView(view: View?) {
        if (view != null) {
            rvMarkParentView = view.findViewById<RecyclerView>(R.id.rv_answer)
        }
        initItemAdapter(rvMarkParentView)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        isCreate = true
    }


    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (isVisibleToUser && isCreate) {
            view?.visibility = View.VISIBLE
        } else {
            view?.visibility = View.INVISIBLE
        }
    }

    private fun initItemAdapter(rvMarkParentView: RecyclerView) {
        initData()
        val manager = LinearLayoutManager(activity)
        manager.orientation = LinearLayoutManager.VERTICAL
        rvMarkParentView.layoutManager = manager
        val mAdapter = MarkFragmentChildView(activity, mAudioTracker, childAnswerList, typeNo)
        rvMarkParentView.adapter = mAdapter
    }

    private fun initData() {
        //遍历所有学生，获取到每个学生的相同答题点的答案

        for (i in stuAnswerList.indices) {
            var stuAnswer = ""
            if (stuAnswerList[i].ChildAnswerList[childIndex].SortIndexType == 0) {
                childAnswerList.add(stuAnswerList[i].ChildAnswerList[childIndex].ItemList[0].apply {
                    ItemAnswer = stuAnswerList[i].ChildAnswerList[childIndex].ItemAnswer
                    StuName = stuAnswerList[i].StuName
                    QuesID = stuAnswerList[i].QuesID
                    StuId = stuAnswerList[i].StuID
                    ChildScore = stuAnswerList[i].ChildAnswerList[childIndex].EvalScore
                    EvalScore = stuAnswerList[i].ChildAnswerList[childIndex].EvalScore
                    TotalScore = stuAnswerList[i].ChildAnswerList[childIndex].TotalScore
                    ChildIndex = stuAnswerList[i].ChildAnswerList[childIndex].ChildIndex
                    Comment = stuAnswerList[i].Comment
                    ImagePath = stuAnswerList[i].ChildAnswerList[childIndex].ImagePath
                    State = stuAnswerList[i].ChildAnswerList[childIndex].State
                    ChildAnswer = stuAnswerList[i].ChildAnswerList[childIndex].StuAnswer
                    if (typeNo == "N") {  //填空题适配
                        val answerList = stuAnswerList[i].ChildAnswerList[childIndex].StuAnswer.split("\$、") //一个小题多个答题点
                        answerList.forEachIndexed { index, s ->
                            var tempS = s
                            if (tempS.isEmpty()) {
                                tempS = "未填写"
                            }
                            if (index < answerList.size - 1) {
                                stuAnswer = "$stuAnswer$tempS、"
                            } else {
                                stuAnswer += tempS
                            }
                        }
                    } else {
                        val itemListSize = stuAnswerList[i].ChildAnswerList[childIndex].ItemList.size
                        stuAnswerList[i].ChildAnswerList[childIndex].ItemList.forEachIndexed { index, itemXMark ->
                            var temps = itemXMark.StuAnswer
                            if (temps.isEmpty()) {
                                temps = "未填写"
                            }
                            if (index < itemListSize - 1) {
                                stuAnswer = "$stuAnswer$temps、"
                            } else {
                                stuAnswer += temps
                            }
                        }
                    }
                    StuAnswer = stuAnswer
                })
            } else {
                if (typeNo == "U") {
                    childAnswerList.add(stuAnswerList[i].ChildAnswerList[childIndex].ItemList[0].apply {
                        StuName = stuAnswerList[i].StuName
                        QuesID = stuAnswerList[i].QuesID
                        StuId = stuAnswerList[i].StuID
                        ChildScore = stuAnswerList[i].ChildAnswerList[childIndex].EvalScore
                        TotalScore = stuAnswerList[i].ChildAnswerList[childIndex].TotalScore
                        ChildIndex = stuAnswerList[i].ChildAnswerList[childIndex].ChildIndex
                        Comment = stuAnswerList[i].Comment
                        ImagePath = stuAnswerList[i].ChildAnswerList[childIndex].ImagePath
                        State = stuAnswerList[i].ChildAnswerList[childIndex].State
                        ChildAnswer = stuAnswerList[i].ChildAnswerList[childIndex].StuAnswer
                    })
                } else {
                    childAnswerList.add(stuAnswerList[i].ChildAnswerList[childIndex].ItemList[index].apply {
                        StuName = stuAnswerList[i].StuName
                        QuesID = stuAnswerList[i].QuesID
                        StuId = stuAnswerList[i].StuID
                        ChildIndex = stuAnswerList[i].ChildAnswerList[childIndex].ChildIndex
                        Comment = stuAnswerList[i].Comment
                        ChildScore = stuAnswerList[i].ChildAnswerList[childIndex].ItemList[index].EvalScore
                    })
                }
            }
        }
    }
}